package com.reflect;

import com.domain.Person;
import com.domain.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//获取成员方法
public class ReflectDemo4 {

    public static void main(String[] args) throws Exception {
        //获取class对象
        Class<Person> personClass = Person.class;
        //获取指定名称的方法
        Method eat = personClass.getMethod("eat");
        Person p = new Person();
        eat.invoke(p);
        //执行方法
        Method eat1 = personClass.getMethod("eat", String.class);
        eat1.invoke(p,"草莓");

        System.out.println("=================");
        //获取所有public方法

        Method[] methods = personClass.getMethods();

        for (Method method : methods) {
            System.out.println(method);
            method.setAccessible(true);
        }
        System.out.println("=================");

        //获取所有方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        System.out.println("=================");
        Method eat2 = personClass.getDeclaredMethod("eat", String.class, String.class);
        eat2.setAccessible(true);
        eat2.invoke(p,"杨琛","雪糕");
        System.out.println("=================");
        //获取方法名
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName());
        }
        System.out.println("=================");
        //获取类吗
        String name = personClass.getName();
        System.out.println(name);
    }




}
